﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class admin_Default : System.Web.UI.Page
{
    private static FilmSchedule filmSchedule = new FilmSchedule();
    private static DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Session["username"] == null)
                Response.Redirect("Login.aspx");
            else
            {
                ds = filmSchedule.LoadAllFilm();
                SetDateTimeForGrid();
            }
        }
    }
    protected override void OnPreRender(EventArgs e) {
        base.OnPreRender(e); 
        GridViewRow pagerRow = GridView1.BottomPagerRow;
        pagerRow.Cells[0].Attributes.Add("align", "center");
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        this.GridView1.SelectedIndex = Int32.Parse(e.CommandArgument.ToString());
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int iFilmId = Int32.Parse(this.GridView1.Rows[this.GridView1.SelectedIndex].Cells[14].Text);
        FilmSchedule fs = new FilmSchedule();
        Boolean iResult = fs.DeleteFilm(iFilmId);
        if (iResult)
        {
            this.lblError.Visible = true;
            this.lblError.Text = "Xóa Phim Thành Công!";
            //LoadFilm();
        }
        else
        {
            this.lblError.Visible = true;
            this.lblError.Text = "Không Thể Xóa Phim, Xin Kiểm Tra Lại Dữ Liệu";
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int iFilmId = Int32.Parse(this.GridView1.Rows[this.GridView1.SelectedIndex].Cells[14].Text);
        Response.Redirect("AddFilm.aspx?fid=" + iFilmId);
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.GridView1.DataBind();
        SetDateTimeForGrid();
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        SetDateTimeForGrid();
    }
    public void SetDateTimeForGrid()
    {
        for (int i = 0; i < this.GridView1.Rows.Count; i++)
        {
            for (int j = 5; j < 14; j++)
            {
                if (DateParse(this.GridView1.Rows[i].Cells[j].Text))
                {
                    if (j == 13)//date show
                        this.GridView1.Rows[i].Cells[j].Text = String.Format("{0:dd/MM/yyyy}", Convert.ToDateTime(this.GridView1.Rows[i].Cells[j].Text));
                    else
                        this.GridView1.Rows[i].Cells[j].Text = String.Format("{0:HH:mm}", Convert.ToDateTime(this.GridView1.Rows[i].Cells[j].Text));
                }
            }
        }
    }
    public Boolean DateParse(string strDate)
    {
        try
        {
            DateTime d = DateTime.Parse(strDate);
            return true;
        }
        catch (System.Exception ex)
        {
            return false;
        }
    }
    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        SetDateTimeForGrid();
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {

    }
}
